package com.superman.libyuvdemo

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager.PERMISSION_GRANTED
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.core.app.ActivityCompat.shouldShowRequestPermissionRationale
import androidx.core.content.ContextCompat

object PermissionUtil {

    private const val TAG = "Permission"

    const val a = Manifest.permission.READ_MEDIA_VIDEO
    const val b = Manifest.permission.READ_MEDIA_AUDIO
    const val c = Manifest.permission.READ_MEDIA_IMAGES


    fun checkAppPermission(
        activity: ComponentActivity,
        launcher: ActivityResultLauncher<String>,
        permission: String,
        resultCallback: (Boolean) -> Unit,
        showRationaleCallback: () -> Unit,
    ) {
        when {
            hasPermission(activity, permission) -> resultCallback(true)
            shouldShowRequestPermissionRationale(activity, permission) -> showRationaleCallback()
            else -> launcher.launch(permission)
        }
    }

    fun hasPermission(context: Context, permission: String): Boolean {
        return ContextCompat.checkSelfPermission(context, permission) == PERMISSION_GRANTED
    }

}